package cn.com.company.main.client.ui;

import java.util.ArrayList;
import java.util.List;

import cn.com.company.main.client.entity.VwWebbomIcitemSimp;
import cn.com.company.main.client.service.GreetingService;
import cn.com.company.main.client.service.GreetingServiceAsync;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.dnd.GridDragSource;
import com.extjs.gxt.ui.client.dnd.GridDropTarget;
import com.extjs.gxt.ui.client.dnd.ListViewDragSource;
import com.extjs.gxt.ui.client.dnd.ListViewDropTarget;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.ListView;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class BomDiffAnalysisWindow extends Window {
	public static BomDiffAnalysisWindow instance;
	private Viewport viewport;
	private ContentPanel topPanel;
	private Button compBtn;
	private Button resetBtn;
	private Grid<VwWebbomIcitemSimp> grid1;
    
	private ContentPanel centerPanel;
	private ListView<VwWebbomIcitemSimp> listView;
	private ListStore<VwWebbomIcitemSimp> listStore;
	
	final GreetingServiceAsync service = GWT.create(GreetingService.class);

	public static BomDiffAnalysisWindow getInstance(String title) {
		if (instance == null) {
			instance = new BomDiffAnalysisWindow(title);
		}
		return instance;
	}

	public BomDiffAnalysisWindow(String title) {
		setIcon(IconHelper.createStyle("icon-grid"));
		setMinimizable(true);
		setMaximizable(true);
		setHeading(title);
		setSize(930, 600);
		setLayout(new FitLayout());
		viewport = new Viewport();
		viewport.setLayout(new BorderLayout());
		createNorthPanel();
		createCenterPanel();
		add(viewport);
	}

	private void createNorthPanel() {
		BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH, 290,
				150, 350);
		northData.setMargins(new Margins(2, 0, 2, 2));
		northData.setCollapsible(true);
		
		TableLayout layout = new TableLayout(3);  
	    layout.setCellSpacing(5);  
		topPanel = new ContentPanel();
		topPanel.setHeading("提示：可拖动需要对比的BOM数据至右边区域，每次只允许比较两份BOM");
		topPanel.setBodyBorder(true);
		topPanel.setAnimCollapse(false);
		topPanel.setLayout(layout);  
		topPanel.setLayoutOnChange(true);
		topPanel.setScrollMode(Scroll.AUTO);
		
		final ListStore<VwWebbomIcitemSimp> store1 = new ListStore<VwWebbomIcitemSimp>();
	    service.getIcItemList(new AsyncCallback<List<VwWebbomIcitemSimp>>() {
			@Override
			public void onSuccess(List<VwWebbomIcitemSimp> list) {
				store1.add(list);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.alert("错误", caught.getLocalizedMessage(), null);
			}
		});
	  
	    grid1 = new Grid<VwWebbomIcitemSimp>(store1, createColumnModel());  
	    grid1.setAutoExpandColumn("fname");  
	    grid1.setBorders(true); 
	    grid1.setTrackMouseOver(false);  
	      
	    ContentPanel cp = new ContentPanel(); 
	    cp.setHeading("BOM列表");
	    cp.setHeaderVisible(false);
	    cp.setSize(670, 250);  
	    cp.setLayout(new FitLayout());  
	    cp.setBodyBorder(false);
	    cp.add(grid1);  

	    topPanel.add(cp);
	    topPanel.add(createListView());
	    topPanel.add(createButtonPanel());
	    
	    new GridDragSource(grid1);  
	    new GridDropTarget(grid1);
	  
		viewport.add(topPanel, northData);
	}
	
	private ContentPanel createListView() {  
	    listView = new ListView<VwWebbomIcitemSimp>();  
	    listView.setBorders(false);  
	    listStore = new ListStore<VwWebbomIcitemSimp>();
	    listView.setStore(listStore);  
	    listView.setDisplayProperty("fBomNumber"); 

	    ContentPanel cp = new ContentPanel();  
	    cp.setSize(160, 250);  
	    cp.setHeading("选择的BOM");
	    cp.add(listView);  
	    cp.setLayout(new FitLayout()); 
	 
	    new ListViewDragSource(listView);
	    new ListViewDropTarget(listView);
	    return cp;
	} 
	
	private ContentPanel createButtonPanel() {  
	    ContentPanel cp = new ContentPanel();  
	    cp.setSize(50, 250);  
	    cp.setHeaderVisible(false);
	    cp.setBodyBorder(false);
	    compBtn = new Button("分&nbsp;&nbsp;&nbsp;&nbsp;析");
	    
	    resetBtn = new Button("重&nbsp;&nbsp;&nbsp;&nbsp;置");
	    resetBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				for(int i=0;i<listStore.getCount();i++){
					VwWebbomIcitemSimp model = listStore.getAt(i);
					if(!grid1.getStore().contains(model)){
						grid1.getStore().add(model);
					}
				}
				listStore.removeAll();
			}
		});
	    cp.setBottomComponent(resetBtn);
	    cp.setTopComponent(compBtn);
	    return cp;
	}  

	
	private void createCenterPanel() {
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(2, 0, 2, 2));
		
		centerPanel = new ContentPanel();
		centerPanel.setBodyBorder(true);
		centerPanel.setHeaderVisible(false);
		centerPanel.setLayout(new FitLayout());
		centerPanel.setLayoutOnChange(true);
		centerPanel.setScrollMode(Scroll.AUTO);
		viewport.add(centerPanel, centerData);
	}
	
	public void show(){
		super.show();
		setSize(935, 605);
	}
	
	private ColumnModel createColumnModel() { 
		DateTimeFormat dateFormat = DateTimeFormat.getFormat("yyyy-MM-dd");
	    List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  
	  
	    ColumnConfig column = new ColumnConfig(); 
		column = new ColumnConfig();
		column.setId("fBomNumber");
		column.setHeader("BOM单编号");
		column.setWidth(80);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("fnumber");
		column.setHeader("物料代码");
		column.setWidth(80);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("fname");
		column.setHeader("物料名称");
		column.setWidth(130);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("fuseStatus");
		column.setHeader("状态");
		column.setWidth(60);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("fstatus");
		column.setHeader("审核");
		column.setWidth(60);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("fversion");
		column.setHeader("版本");
		column.setWidth(40);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("fenterTime");
		column.setHeader("最后更新日期");
		column.setWidth(85);
		column.setDateTimeFormat(dateFormat);
		
	    column = new ColumnConfig();
	    column.setId("finterId");
		column.setHeader("InterID");
		column.setWidth(50);
		column.setHidden(true);
		column.setRowHeader(true);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("fitemId");
		column.setHeader("ItemID");
		column.setWidth(50);
		column.setHidden(true);
		configs.add(column);
		
		configs.add(column);

	    return new ColumnModel(configs);  
	  }  

}
